<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>汽车管理</title>
    <!-- 引入必要的样式和脚本 -->
    <div th:replace="header::common"></div>
    <style>
        .center {
            padding-left: 5px;
            padding-right: 5px;
            padding-top: 5px;
        }
        .pagination {
            display: inline-block;
            padding-left: 0px;
            margin: 0px 0px;
            border-radius: 4px;
        }
        .table {
            width: 100%;
            max-width: 100%;
            margin-bottom: 10px;
            margin-top: 10px;
        }
        .narrow-dropdown {
            width: 150px; /* 根据需要调整宽度 */
        }
    </style>
</head>
<body class="center">
<a class="btn btn-sm btn-primary" href="/car/toAdd">添加</a>
<a class="btn btn-sm btn-danger" th:onclick="deleteAll()">批量删除</a>
<!--搜索框-->
<div>
    <label></label><br>
    <label for="search">查询条件：</label><br>
    <label style="margin-left: 20px;">车牌:</label>
    <input type="text" id="search" name="carp" placeholder="请输入车牌信息" th:value="${car.carp}">

    <label style="display: inline-block; margin-left: 20px;">型号名:</label>
    <select class="form-control narrow-dropdown" name="carid" style="display: inline-block;">
        <option th:value="0" th:text="请选择"></option>
        <th:block th:each="carType:${list}">

            <option th:selected="${carType.id==car.carid}" th:value="${carType.id}" th:text="${carType.name}"></option>
        </th:block>
    </select>
    <label style="margin-left: 20px;">租金:</label>
    <input type="text" id="rent" name="rent" placeholder="请输入租金" th:value="${car.rent != '0' ? car.rent : ''}">
    <label style="margin-left: 20px;">是否出租:</label>
    <input type="radio" name="status" value="1" th:checked="${car.status == 1 or car.status == null}" style="margin-left: 20px;"> 未出租
    <input type="radio" name="status" value="2" th:checked="${car.status == 2 }" style="margin-left: 20px;"> 已出租
    <input type="radio" name="status" value="3" checked  th:checked="${car.status == 3 }" style="margin-left: 20px;"> 不选

    <button onclick="search()" style="
    background-color: #4CAF50; /* 绿色背景 */
    color: white; /* 白色文字 */
    padding: 10px 10px; /* 内部填充 */
    border-radius: 5px; /* 圆角边框 */
    text-align: center; /* 文字居中 */
    display: inline-block; /* 行内块显示 */
    font-size: 12px; /* 字体大小 */
    margin-left: 40px; /* 左边距 */
    box-shadow: 0 4px #999; /* 阴影效果 */
">
        查询
    </button>
</div>

<!-- Thymeleaf 静态表格 -->
<table class="table table-striped table-bordered table-hover table-condensed">
    <tr class="success">
        <td><input type="checkbox" id="checkAll"></td>
        <td>ID</td>
        <td>车牌</td>
        <td>型号</td>
        <td>型号名</td>
        <td>照片</td>
        <td>价值(万)</td>
        <td>已跑里程</td>
        <td>租金</td>
        <td>押金</td>
        <td>更新时间</td>
        <td>状态</td>
        <td>详细内容</td>
        <td>操作</td>
    </tr>
    <tr th:each="carVO:${pageInfo.list}">
        <td><input type="checkbox" th:value="${carVO.id}" class="cb"></td>
        <td th:text="${carVO.id}"></td>
        <td th:text="${carVO.carp}"></td>
        <td th:text="${carVO.carid}"></td>
        <td th:text="${carVO.typeName}"></td>
        <td><img width="40px" height="35" th:src="|/img/${carVO.image}|"></td>
        <td th:text="${carVO.value}"></td>
        <td th:text="${carVO.count}"></td>
        <td th:text="${carVO.rent}"></td>
        <td th:text="${carVO.deposit}"></td>
        <td th:text="${#dates.format(carVO.gmtModified, 'yyyy-MM-dd HH:mm')}"></td><!-- HH-24制，hh12制 !-->
        <td>
            <a class="layui-btn-radius btn btn-success" th:if="${carVO.status==1}">未出租</a>
            <a class="layui-btn-radius btn btn-warning" th:if="${carVO.status==2}">已出租</a>
        </td>
        <td>
            <a class="layui-btn-radius btn btn-success" th:onclick="showCarContent([[${carVO.content}]])">查看内容</a>
        </td>
        <td>
            <a class="layui-btn-radius btn btn-danger" th:onclick="|deleteById(${carVO.id})|">删除</a>
            <a class="layui-btn-radius btn btn-primary" th:href="|/car/toUpdate?id=${carVO.id}|">编辑</a>
        </td>
    </tr>
</table>
<nav aria-label="Page navigation">
    <ul class="pagination">
        <!--上一页-->
        <th:block th:if="${pageInfo.pageNo==1}">
            <li class="disabled">
                <a href="#" aria-label="Previous">
                    <span aria-hidden="true">上一页</span>
                </a>
            </li>
        </th:block>
        <th:block th:if="${pageInfo.pageNo!=1}">
            <li>
                <a th:href="|/car/selectByPage?pageNo=${pageInfo.pageNo-1}&carp=${car.carp}&carid=${car.carid}&rent=${car.rent}&status=${car.status}|" aria-label="Previous">
                    <!--<span aria-hidden="true">&laquo;</span>-->
                    <span aria-hidden="true">上一页</span>
                </a>
            </li>
        </th:block>

        <li th:class="${pageInfo.pageNo==page}?active:''" th:each="page:${#numbers.sequence(1, pageInfo.totalPage)}">
            <th:block th:if="${pageInfo.pageNo==page}">
                <a href="#" th:text="${page}"></a>
            </th:block>
            <th:block th:if="${pageInfo.pageNo!=page}">
                <a th:href="|/car/selectByPage?pageNo=${page}&carp=${car.carp}&carid=${car.carid}&rent=${car.rent}&status=${car.status}|" th:text="${page}"></a>
            </th:block>
        </li>

        <!--下一页-->
        <th:block th:if="${pageInfo.pageNo==pageInfo.totalPage}">
            <li class="disabled">
                <a href="#" aria-label="Next">
                    <span aria-hidden="true">下一页</span>
                </a>
            </li>
        </th:block>
        <th:block th:if="${pageInfo.pageNo!=pageInfo.totalPage}">
            <li>
                <a th:href="|/car/selectByPage?pageNo=${pageInfo.pageNo+1}&carp=${car.carp}&carid=${car.carid}&rent=${car.rent}&status=${car.status}|" aria-label="Next">
                    <!-- <span aria-hidden="true">&raquo;</span>-->
                    <span aria-hidden="true">下一页</span>
                </a>
            </li>
        </th:block>
    </ul>
</nav>
<script>
    function search() {

        var carp = encodeURIComponent(document.getElementById("search").value);
        var carid = encodeURIComponent(document.querySelector('select[name="carid"]').value);
        var rent = encodeURIComponent(document.getElementById("rent").value);


        var statusRadios = document.getElementsByName("status");
        // 定义一个变量来存储选中的单选框的值
        var selectedStatusValue = null;
        // 遍历单选框元素，找到被选中的单选框
        for (var i = 0; i < statusRadios.length; i++) {
            if (statusRadios[i].checked) {
                selectedStatusValue = statusRadios[i].value;
                break;  // 找到被选中的单选框后结束循环
            }
        }

        var nextPageUrl = '/car/selectByPage?carp=' + carp + '&carid=' + carid + '&rent=' + rent + '&status=' + selectedStatusValue;

        // Redirect the user to the URL
        window.location.href = nextPageUrl;

    };
    $(function (){
        $('#checkAll').click(function (){
            $('.cb').prop('checked', this.checked);
        });
    });

    function showCarContent(content) {
        layer.open({
            type: 1,
            area: ['1000px', '450px'],
            content: content
        })
    }
    function deleteAll() {
        layer.confirm('您确认要删除么?', function() {
            let array = new Array();
            $('.cb:checked').each(function() {
                array.push($(this).val());
            })
            if (array.length==0){
                mylayer.errorMsg('至少要选择一个');
                return;
            }
            location.href = '/car/deleteAll?ids=' + array;
        });
    }
    function deleteById(id){
        layer.confirm('您确定要删除吗？',function (){
            location.href='/car/deleteById?id='+id;
        });
    }
</script>
</body>
</html>
